﻿using System;
using Autopoco.Contrib.DataSources.Enums;
using AutoPoco.Engine;

namespace Autopoco.Contrib.DataSources
{
    public class ToStringSource<TSource, T> : DatasourceBase<string> where TSource : DatasourceBase<T>
    {
        private readonly bool _staticLenght;
        private readonly int _lenght;
        private readonly Padding _padding;
        private readonly char _ch;
        private readonly TSource _source;

        public ToStringSource(bool staticLenght, int lenght, Padding padding, char ch, params object[] param)
        {
            _staticLenght = staticLenght;
            _lenght = lenght;
            _padding = padding;
            _ch = ch;
            _source = Activator.CreateInstance(typeof(TSource), param) as TSource;
        }

        public ToStringSource()
            : this(false, 0, Padding.Left, '0')
        {
        }


        public override string Next(IGenerationSession session)
        {
            return !_staticLenght
                       ? _source.Next(session).ToString()
                       : _padding == Padding.Left
                             ? _source.Next(session).ToString().PadLeft(_lenght, _ch).Substring(0, _lenght)
                             : _source.Next(session).ToString().PadRight(_lenght, _ch).Substring(0, _lenght);
        }
    }
}